<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../head.jsp"%>
			<noscript>
				<div class="alert alert-block span10">
					<h4 class="alert-heading">Warning!</h4>
					<p>You need to have <a href="http://en.wikipedia.org/wiki/JavaScript" target="_blank">JavaScript</a> enabled to use this site.</p>
				</div>
			</noscript>
			
<div id="content" class="span10"><!-- content starts -->
	<div class="row-fluid sortable">		
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i>我的商家</h2>
				<div class="box-icon">
					<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
					<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
					<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
				</div>
			</div>
			<div class="box-content">
				<table class="table table-striped table-bordered bootstrap-datatable datatable">
					<tr>
					  	<th>商家ID</th>
					  	<th>商家名称</th>
					  	<th>状态</th>
					</tr>
				  	<tbody id="myShopList"></tbody>
				</table>
			</div><!-- box-content end -->
		</div><!-- box span12 end  -->
	</div><!-- row-fluid sortable end  -->
	
	<div class="row-fluid sortable">		
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i>商家列表</h2>
				<div class="box-icon">
					<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
					<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
					<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
				</div>
			</div>
			<div class="box-content">
				<table class="table table-striped table-bordered bootstrap-datatable datatable">
					<tr>
					  	<th>商家ID</th>
					  	<th>商家名称</th>
					  	<th>申请时间</th>
					  	<th>操作</th>
					</tr>
				  	<tbody id="shopList"></tbody>
				</table>
			</div><!-- box-content end -->
		</div><!-- box span12 end  -->
	</div><!-- row-fluid sortable end  -->
</div>
<script type="text/javascript">
<!--
	$(function (){
		getMyShopList = function(){
			$("#myShopList").html("");
			wtj.ajax({
				url : "<%=basePath%>/shop_myShopList/${user.id }",
				success : function(response){
					var info = "" ;
					$.each(response.shoplist, function(i,itme) {
					  	info +="<tr>";
					  	info +="<td>"+itme.ID+"</td>";
					  	info +="<td>"+itme.SHOPNAME+"</td>";
					  	if(itme.STATUS==1){
					  		info +="<td>已审批</td>";
					  	}else{
					  		info +="<td>未审批</td>";
					  	}
					  	
						info +="</tr>";
					});
					 $("#myShopList").append(info);    
				}
			});
		}
		getMyShopList();
	});
	$(function (){
		getShopList = function(){
			$("#shopList").html("");
			wtj.ajax({
				url : "<%=basePath%>/shop_getShopList",
				success : function(response){
					var info = "" ;
					$.each(response.shoplist, function(i,itme) {
					  	info +="<tr>";
					  	info +="<td>"+itme.ID+"</td>";
						info +="<td>"+itme.SHOPNAME+"</td>";
						info +="<td>"+itme.CREATERTIME+"</td>";
						info +="<td><a href='javascript:_applyMyShop(\""+itme.ID+"\", \"" + itme.SHOPNAME + "\");'>申请</a></td>";
						info +="</tr>";
					});
					 $("#shopList").append(info);    
				}
			});
		}
		getShopList();
	});
	
		// 显示增加自选股 dialog
	function _applyMyShop(shopid, shopName) {
		$("#shopid").val(shopid);
		$("#shopName").val(shopName);
		$("#usid").val("${user.id }");
		$("#userName").val("${user.name }");
		$("#applyShop").modal("show");
		$("#usadressid").val("");   //清空addressId
		$("#loadAddress").html(""); //清空select
		_chooseAddress(null, '-1');
	}
	
	function _chooseAddress(obj, addressId) {
		if (addressId == '0') {
			removeNode(obj);
			setAddressID(obj);
			return;
		} else {
			if (obj != null) {
				removeNode(obj);
			}
			$.ajax({
					url : "<%=basePath%>/address_getRelatedAddressList/" + addressId,
					beforeSend  : function() {$("#loadMsg").text("加载中...");}, 
					success : function(response) {
						$("#loadMsg").text("");
						var len = response.addressList.length;
						var addressLen = $("#loadAddress").children("select").length;
						var info = "";
						if (len > 0) {
							info = "<select id='addressSel"+ (addressLen + 1) +"' name='addressSel' onchange='javascript:_chooseAddress(this, this.value);'>\n";
					    	info += "<option value='0'>-请选择-</option>\n";
					    	$.each(response.addressList, function(i, item) {
							  	info += "<option value='" + item.id + "'>";
							  	info += item.name;
								info +="</option>\n";
							});
							info += "</select>\n";
						}
						$("#loadAddress").append(info);
						if (addressId != "-1") {
							setAddressID(obj);
						}
					}
			});
		}
	}
	
	function removeNode(obj) {
		var id = obj.id;
		var index = id.substring(10, id.length);
		var _id = null, _index = null;
		$("#loadAddress").children().each(function (i, item) {
			_id = $(this).attr("id");
			_index = _id.substring(10, _id.length);
			if (parseInt(index) < parseInt(_index)) {
				$(this).remove();
			}
		});
	}
	
	function setAddressID(obj) {
		var id = obj.id;
		var index = parseInt(id.substring(10, id.length));
		if (obj.value == "0") {
			if (index > 1) {
				$("#usadressid").val($("#addressSel" + (index - 1)).val()); //父节点的ADDRESSID
			} else {
				$("#usadressid").val("");
			}
		} else {
			$("#usadressid").val(obj.value);
		}
	}
//-->
</script>	
<%@ include file="../../bottom.jsp" %>

<!-- dialog -->
	<script type="text/javascript">
		function _saveApplyShop() {
			$.post('<%=basePath%>/shop_saveApplyShop',
				$('#_applyShopForm').serialize(), 
				function(response) {
					$("#applyShop").modal("hide");
					getMyShopList();
				}, "json");
		}
	</script>
	<form id="_applyShopForm">
		<input type="hidden" id="usid" name="usid" />
		<input type="hidden" id="shopid" name="shopid" />
		<input type="hidden" id="usadressid" name="usadressid" />
		<div class="modal hide fade" id="applyShop">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal">×</button>
				<h3>申请商家</h3>
			</div>
			<div class="modal-body">
				<p>商家：<input type="text" id="shopName" name="shopName" /></p>
				<p>用户：<input type="text" id="userName" name="userName" /></p>
				<p>所在地区：
					<span id="loadAddress"></span>
					<span id="loadMsg"></span>
				</p>
			</div>
			<div class="modal-footer">
				<a href="#" class="btn" data-dismiss="modal">关闭</a>
				<a href="javascript:_saveApplyShop();" class="btn btn-primary">保存</a>
			</div>
		</div>
	</form>
